package com.macro.mall.service.impl;

import com.macro.mall.dto.UmsPermissionNode;
import com.macro.mall.mapper.UmsPermissionMapper;
import com.macro.mall.model.UmsPermission;
import com.macro.mall.model.UmsPermissionExample;
import com.macro.mall.service.UmsPermissionService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 后台用户权限管理Service实现类
 * Created by macro on 2018/9/29.
 */
@Service
public class UmsPermissionServiceImpl implements UmsPermissionService {
    @Autowired
    private UmsPermissionMapper permissionMapper;

    @Override
    public int create(UmsPermission permission) {
        permission.setStatus(1);
        permission.setCreateTime(new Date());
        permission.setSort(0);
        return permissionMapper.insert(permission);
    }

    @Override
    public int update(Long id, UmsPermission permission) {
        permission.setId(id);
        return permissionMapper.updateByPrimaryKey(permission);
    }

    @Override
    public int delete(List<Long> ids) {
        UmsPermissionExample example = new UmsPermissionExample();
        example.createCriteria().andIdIn(ids);
        return permissionMapper.deleteByExample(example);
    }

    @Override
    public List<UmsPermissionNode> treeList() {
        List<UmsPermission> permissionList = permissionMapper.selectByExample(new UmsPermissionExample());
        if (CollectionUtils.isNotEmpty(permissionList)) {
            Map<Long, List<UmsPermission>> umsPermissionMap = permissionList.stream().collect(Collectors.groupingBy(UmsPermission::getPid));
            return recursiveConvertToTree(0L, umsPermissionMap);
        }
        return new ArrayList<>(1);
    }

    @Override
    public List<UmsPermission> list() {
        return permissionMapper.selectByExample(new UmsPermissionExample());
    }

    /**
     * 将权限转换为带有子级的权限对象
     * 当找不到子级权限的时候map操作不会再递归调用covert
     */
    private UmsPermissionNode covert(UmsPermission permission, List<UmsPermission> permissionList) {
        UmsPermissionNode node = new UmsPermissionNode();
        BeanUtils.copyProperties(permission, node);
        List<UmsPermissionNode> children = permissionList.stream()
                .filter(subPermission -> subPermission.getPid().equals(permission.getId()))
                .map(subPermission -> covert(subPermission, permissionList)).collect(Collectors.toList());
        node.setChildren(children);
        return node;
    }

    /**
     * 递归构建树
     * <code>
     * <p>
     * BeanUtils 性能过慢 ,可以考虑引入 ModelMapper ???
     *
     * @param pid              pid
     * @param umsPermissionMap 根据pid 分组好的map
     * @return 树
     * @Resource private ModelMapper modelMapper;
     * <p>
     * // copy properties
     * UmsPermissionNode umsPermissionNode =  modelMapper.map(umsPermission,UmsPermissionNode.class);
     *
     * </code>
     */
    private List<UmsPermissionNode> recursiveConvertToTree(long pid, Map<Long, List<UmsPermission>> umsPermissionMap) {
        List<UmsPermission> umsPermissions = umsPermissionMap.get(pid);
        if (CollectionUtils.isNotEmpty(umsPermissions)) {
            return umsPermissions.stream().map(umsPermission -> {
                UmsPermissionNode umsPermissionNode = new UmsPermissionNode();
                BeanUtils.copyProperties(umsPermission, umsPermissionNode);
                umsPermissionNode.setChildren(recursiveConvertToTree(umsPermission.getId(), umsPermissionMap));
                return umsPermissionNode;
            }).collect(Collectors.toList());
        }
        return new ArrayList<>(1);
    }

}
